/**
 *project huodong
 *com.yysoft.util.StringHelper.java 
 *copyrite@ by admin@xkshow.cn
 ****************************************************************
 *         VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *          1.0          Apr 10, 2013     hardy         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package com.yysoft.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

public class StringHelper {

    public StringHelper()
    {
    }

    public static String escapeHTMLTags(String input)
    {
        if(input == null || input.length() == 0)
            return input;
        StringBuffer buf = new StringBuffer(input.length() + 6);
        char ch = ' ';
        for(int i = 0; i < input.length(); i++)
        {
            ch = input.charAt(i);
            if(ch == '<')
                buf.append("&lt;");
            else
            if(ch == '>')
                buf.append("&gt;");
            else
            if(ch == '"')
                buf.append("&quot;");
            else
            if(ch == '\'')
                buf.append("&#39;");
            else
                buf.append(ch);
        }

        return buf.toString();
    }

    private static String replace(String line, String oldString, String newString)
    {
        if(line == null)
            return null;
        int i = 0;
        if((i = line.indexOf(oldString, i)) >= 0)
        {
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for(j = i; (i = line.indexOf(oldString, i)) > 0; j = i)
            {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            return buf.toString();
        } else
        {
            return line;
        }
    }

    public static String getQueryString(HttpServletRequest request, String key)
    {
        return getQueryString(request, key, "");
    }

    public static String getQueryString(HttpServletRequest request, String key, String defaultValue)
    {
        String rtnValue = request.getParameter(key);
        if(rtnValue == null || rtnValue.trim().length() == 0)
            rtnValue = defaultValue;
        rtnValue = toGBK(rtnValue);
        return rtnValue.trim();
    }

    public static String toGBK(String source)
    {
        if(source == null)
            return null;
        if(source.trim().equals(""))
            return source;
        String retStr = source;
        try
        {
            byte b[] = source.getBytes("ISO8859_1");
            for(int i = 0; i < b.length; i++)
            {
                byte b1 = b[i];
                if(b1 == 63)
                    break;
                if(b1 > 0 || b1 >= 0)
                    continue;
                retStr = new String(b, "GBK");
                break;
            }

        }
        catch(UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return retStr;
    }

    public static String[] toGBK(String source[])
    {
        String ret[] = new String[source.length];
        for(int i = 0; i < source.length; i++)
            ret[i] = toGBK(source[i]);

        return ret;
    }

    public static boolean isNotNull(String value)
    {
        return value != null && !"".equals(value.trim());
    }

    public static boolean isNotNull(Object value)
    {
        if(value != null)
            return !(value instanceof String) || !"".equals(((String)value).trim());
        else
            return false;
    }

    public static boolean isNotNull(String value[])
    {
        return value != null && value.length != 0;
    }

    public static String array2String(String args[], String arg)
    {
        StringBuffer sb = new StringBuffer();
        if(args == null || args.length <= 0)
            return "";
        for(int i = 0; i < args.length; i++)
            if(args[i] == null || args[i].trim().equals(""))
                sb.append((new StringBuffer(String.valueOf(arg))).toString());
            else
                sb.append(arg + args[i]);

        if(sb.length() > 0)
            return sb.substring(arg.length());
        else
            return sb.toString();
    }

    public static String truncateString(String source, int maxLength, String substitute)
    {
        if(isNotNull(source))
        {
            char array[] = source.toCharArray();
            int length = 0;
            for(int i = 0; i < array.length; i++)
            {
                char c = array[i];
                if(c <= 0 || c >= '~')
                    length += 2;
                else
                    length++;
            }

            if(length > maxLength && source.length() > maxLength)
                return source.substring(0, maxLength) + substitute;
        }
        return source;
    }

    public static String toPlainText(String input)
    {
        if(input == null)
        {
            return "";
        } else
        {
            input = input.replaceAll("</?[^>]+>", "");
            return input;
        }
    }

    public static Integer[] convert2IntegerArray(String strValues[])
        throws ClassCastException
    {
        if(strValues == null || strValues.length == 0)
            return null;
        Integer integerValues[] = new Integer[strValues.length];
        for(int i = 0; i < strValues.length; i++)
            integerValues[i] = Integer.valueOf(strValues[i]);

        return integerValues;
    }

    public static Long[] convert2LongArray(String strValues[])
        throws ClassCastException
    {
        if(strValues == null || strValues.length == 0)
            return null;
        Long longValues[] = new Long[strValues.length];
        for(int i = 0; i < strValues.length; i++)
            longValues[i] = Long.valueOf(strValues[i]);

        return longValues;
    }

    public static String toHtmlText(String input)
    {
        String ret = escapeHTMLTags(input);
        ret = replace(ret, "\r\n", "<br>");
        ret = replace(ret, "\n", "<br>");
        ret = replace(ret, "\r", "<br>");
        return ret;
    }

    public static String textToHtml(String input)
    {
        return toHtmlText(input);
    }

    public static void main(String args1[])
    {
    }
}
